DSA - Data Structures and Algorithms
-
1 August 2025
String Manipulation
-
Reverse a String
- 2 pointers: Start and End or Left and Right
-
Algorithm:
- Initialize two pointers: one at the start and one at the end of the list. Start=0 and End=len(string)-1
- Repeat until start < end. Swap the character of start and end. Increment start, decrement end.
- After loop ends, string is reversed.
- Return or print the reversed string.
-
Time Complexity: O(n)
-
Check if a String is a Palindrome
- A palindrome is a string that reads the same backward as forward (e.g., "madam", "racecar").
-
Algorithm:
- Reverse the string.
- Compare the original string with its reversed version.
- If both are equal, the string is a palindrome; otherwise, it is not.
-
Time Complexity: O(n)
-
Count Vowels and Consonants
- Iterate through each character in the string.
- Use a counter for vowels and another for consonants.
-
Algorithm:
- Initialize two counters: vowels = 0, consonants = 0.
- Loop through each character in the string.
- For each character, check if it is a letter.
- If the letter is a vowel (a, e, i, o, u, A, E, I, O, U), increment vowels.
- If the letter is a consonant, increment consonants.
- Return or print the counts of vowels and consonants.
-
Time Complexity: O(n)
n=length of the string
-
Find the First Non-Repeating Character
- Identifies the first character in a string that does not repeat.
-
Algorithm:
- Traverse the string to count occurrences of each character.
- Iterate again to find the first character with a count of one.
-
Time Complexity: O(n)
-
Anagram Check
- Determines if two strings are anagrams (contain the same characters in any order).
-
Algorithm:
- Sort both strings and compare, or count character frequencies and compare.
-
Time Complexity: O(n)
-
String Compression
- Compresses a string by replacing sequences of the same character with the character followed by the count.
-
Algorithm:
- Iterate through the string, count consecutive characters, and build the compressed string.
-
Time Complexity: O(n)
-
Longest Substring Without Repeating Characters
- Finds the length of the longest substring without repeating characters.
-
Algorithm:
- Use a sliding window with a hash set to track unique characters.
-
Time Complexity: O(n)
-
Capitalize First Letter Each Word In a String
- Capitalizes the first letter of each word in a given string.
-
Algorithm:
- Split the string into words, capitalize the first letter of each, and join them back.
-
Time Complexity: O(n)
-
4 August 2025
5 August 2025
Array Manipulation